package me.zhengjie.modules.quartz.task;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.domain.Msg;
import me.zhengjie.business.repository.GsRepository;
import me.zhengjie.business.repository.MsgRepository;
import me.zhengjie.business.repository.ZhsqRepository;
import me.zhengjie.weixin.WxTemplateBweqI68;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 自动生成订单任务
 * @author xiahongbo
 * @date 2022-07-20
 */
@Slf4j
@Async
@Component
public class MsgTask {

    @Autowired
    private MsgRepository msgRepository;

    private static Timestamp LAST_TIME = null;

    private final static int MAX_TIMES = 3;

    @Transactional
    public void sendMsg() {

        //查询所有符合条件的消息，1.发送时间在当前时间之前 2.发送时间在上次时间LAST_TIME以后
        // 3.发送状态为待发送 0 ，4.发送次数小于最大发送次数
        List<Msg> msgList = msgRepository.querySendMsg("0",new Timestamp(System.currentTimeMillis()),MAX_TIMES);

        for (Msg msg : msgList){
            switch (msg.getType()) {
                case "WxTemplate" :
                    try {
                        String contentStr = msg.getContent();

                        log.info("发送微信模板消息contentStr {} ",contentStr);
                        JSONObject content = JSONObject.parseObject(contentStr);
                        log.info("发送微信模板消息content {} ",content);
                        int sendMsgResult = WxTemplateBweqI68.creat(content.getString("first"),content.getString("keyword1"),
                                        content.getString("keyword2"),content.getString("keyword3"),
                                        content.getString("remark"))
                                .sendMsg(msg.getTarget(),"https://app.cczhaohu.com");
                        if(sendMsgResult != 0){
                            msg.setErrorMsg("发送失败。");
                            msg.setTimes(msg.getTimes()+1);
                            if(msg.getTimes() >= MAX_TIMES){
                                msg.setStatus("-1");
                            }
                            msgRepository.save(msg);
                        }else {

                            msg.setErrorMsg("发送成功。");
                            msg.setTimes(msg.getTimes()+1);
                            msg.setStatus("1");
                            msgRepository.save(msg);
                        }
                    }catch (Exception e){
                        msg.setErrorMsg("发送失败。");
                        msg.setTimes(msg.getTimes()+1);
                        if(msg.getTimes() >= MAX_TIMES){
                            msg.setStatus("-1");
                        }
                        msgRepository.save(msg);
                    }
                    break;
                default:
                    msg.setTimes(MAX_TIMES);
                    msg.setStatus("-1");
                    msg.setErrorMsg("未知消息类型");
                    msgRepository.save(msg);
            }
        }


    }


}
